import { createRouter, createWebHistory } from 'vue-router'


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'root',
      redirect: '/index ',
      meta: { requiresAuth: false }
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/AuthPage.vue'),
      meta: { requiresAuth: false }
    },
    {
      path: '/home',
      name: 'home',
      component: () => import('../views/home/index.vue'),
      meta: { requiresAuth: true, keepAlive: true },
      children: [
        // 控制台
        {
          path: 'main',
          name: 'main',
          component: () => import('@/views/main/index.vue'),
          meta: { menuIndex: '1', keepAlive: false } // 与菜单index对应
        },
        // 用户管理
        {
          path: 'user',
          name: 'UserManagement',
          component: () => import('@/views/user/index.vue'),
          meta: { menuIndex: '2', keepAlive: false }
        },
        // 订单管理
        {
          path: 'order',
          name: 'OrderManagement',
          component: () => import('@/views/order/index.vue'),
          meta: { menuIndex: '3', keepAlive: false }
        },
        // 商品管理
        {
          path: 'goods',
          name: 'GoodsManagement',
          component: () => import('@/views/goods/index.vue'),
          meta: { menuIndex: '4', keepAlive: true, scrollRestoration: 'auto' },
        },
        // 数据统计
        {
          path: 'analytics',
          name: 'DataAnalytics',
          component: () => import('@/views/analytics/index.vue'),
          meta: { menuIndex: '5', keepAlive: false }
        },
        // 消息中心
        {
          path: 'notifications',
          name: 'Notifications',
          component: () => import('@/views/notifications/index.vue'),
          meta: { menuIndex: '6', keepAlive: false }
        },
        // // 系统设置
        // {
        //   path: 'settings',
        //   name: 'SystemSettings',
        //   component: () => import('@/views/settings/index.vue'),
        //   meta: { menuIndex: '7', keepAlive: false }
        // },
        // // 权限管理
        // {
        //   path: 'permissions',
        //   name: 'PermissionManagement',
        //   component: () => import('@/views/permissions/index.vue'),
        //   meta: { menuIndex: '8', keepAlive: false }
        // },
        // // 日志查看
        // {
        //   path: 'logs',
        //   name: 'LogViewer',
        //   component: () => import('@/views/logs/index.vue'),
        //   meta: { menuIndex: '9', keepAlive: false }
        // },
        {
          path: 'phone',
          name: 'phone',
          component: () => import('@/views/phone/index.vue'),
          meta: { menuIndex: '10', keepAlive: false }
        }
      ]
    },
    {
      // 帖子
      path: '/post/:id',
      name: 'post',
      component: () => import('@/views/post/index.vue'),
      props: true // 将路由参数作为 props 传递给组件
    },
    {
      // 角色
      path: '/character/:id',
      name: 'character',
      component: () => import('@/views/characterDetail/index.vue'),
      props: true // 将路由参数作为 props 传递给组件
    },
    {
      path: '/user/post',
      name: 'UserPost',
      component: () => import('@/views/user/UserPost.vue'),
      meta: { menuIndex: '11', keepAlive: false }
    },
    {
      path: '/user/character',
      name: 'UserCharacter',
      component: () => import('@/views/user/UserCharacter.vue'),
      meta: { menuIndex: '12', keepAlive: false }
    }
  ],
})

router.beforeEach((to, from, next) => {
  if (to.name !== 'login' && !localStorage.getItem('access_token')) next({ name: 'login' })
  else next()
})
router.js


export default router
